<extend name="layout/main" />

<block name="HEADER">
	
</block>

<block name="CONTENT">
	<div class="wrapper">
		<div class="panel-heading">
			<control:search id="searchs" />
		</div>
		<div class="content">
			<control:grid id="grid" padding="0" height="100%" />
		</div>
	</div>
</block>

<block name="FOOTER">
	<script type="text/javascript">
		var page = (function(ns){
     
		    //页面初始化
		    ns.init=function(){
		        ns.query();
		    }
		    
		    //搜索事件
		    ns.query=function(){
		    	grid.load( searchs.getData() );
		    }
		    
		    //新增
		    ns.add=function(){
		    	var filter = {
		            url:"modules/goods/cate/edit",
		            oid:"",
		            mode:1
		        }
		        $.openIframe(filter, "新增",['650px', '580px']);
		    }
		    
		    //编辑
		    ns.edit=function(){
		    	var selectedRow = grid.getSelectedRows()[0];
		        var filter = {
		            url:"modules/goods/cate/edit",
		            oid: selectedRow.id ,
		            mode:2
		        }
		        $.openIframe(filter, "编辑",['650px', '580px']);
		    }  
		    
		    //删除
		    ns.del =function(){
		    	grid.deleteSelectRow();
		    }
		    
		    //下拉移动  type: 1 = 上移 ,  type: 2 = 下移
		    ns.setShow = function(id,type){
		    	grid.updateField(id,'is_show',type);
		    }
		    
		    
		    //移动
		    ns.move=function(mode){
		        var grids = grid;
		        var selectedRow=grids.getSelectedRows();
		        if(!selectedRow[0]){
		            $.toast(0,"请先选择数据！");
		            return;
		        }
		        if(selectedRow.length>1){
		            $.toast(0,"只能选择一条数据！");
		            return;
		        }
		        var rows = $.map(grids.getRows(),h=>{
		            if(selectedRow[0]._level == h._level && selectedRow[0].pid == h.pid) return h;
		        });
		        var coverId=-1;
		        rows.forEach(function(h,index){
		            if(selectedRow[0].id == h.id){
		                if(mode==0){
		                    if(rows[index-1]){
		                        coverId = rows[index-1].id;
		                    }
		                }else{
		                    if(rows[index+1]){
		                        coverId = rows[index+1].id;
		                    }
		                }
		            }
		        });
		        var param={ moveId:selectedRow[0].id, coverId:coverId}
		        $.ajaxs("/admin/v1/goods_cate/sortMove",param,function(res){
		            grids.load()
		        });
		    }
		    
		    
		    return ns;
		}({}));
		
		
		$(function(){ page.init();  });

	</script>
</block>